import { createStore } from 'vuex'
import { DOLOGIN } from '../api/request'
import { ElMessage } from 'element-plus'
import { set__token } from '../plugins/storege'
export default createStore({
  state: {
    token: '',
  },
  mutations: {
    SET_TOKEN(state, token) {
      state.token = token
      set__token(token)
    },
  },
  actions: {
    async requestLogin({ commit }, data) {
      // 发送请求
      try {
        const result = await DOLOGIN(data.params)
        if (result.meta.status === 200) {
          ElMessage.success('登陆成功')
          commit('SET_TOKEN', result.data.token)
          // 跳转到首页
          data.goHome()
        }
      } catch (error) {
        console.log(error)
        ElMessage.error('登陆失败')
      }
    },
  },
})
